<template>
    <h2>登录</h2>
    <table>
        <tr>
            <td>用户名：</td>
            <td>
                <input type="text" v-model="UserName" />
            </td>
        </tr>
        <tr>
            <td>用户密码：</td>
            <td>
                <input type="password" v-model="Password" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <button @click="register">登录</button>
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
//创建仓库
import { useUserStore } from '../../store/pinia.ts'
const store = useUserStore();

const UserName = ref('');
const Password = ref('');
const router = useRouter();
const register = () => {
    axios.get("http://localhost:50394/api/Role/UserLogin", {
        params: {
            name: UserName.value,
            pwd: Password.value
        }
    })
        .then(res => {
            if (res.data != null) {
                alert("登录成功");
                searchtype(res.data.Uid);

            }
            else {
                alert("登录失败");
                return;
            }
        })
        .catch(err => {
            console.log(err);
        })
}
//根据类型跳转页面
const searchtype = (Uid: any) => {
    axios.get("http://localhost:50394/api/Role/PanType", {
        params: {
            Uid: Uid
        }
    })
        .then(res => {
            if (res.data[0].UR_Quanxian == "用户") {
                store.userid = "用户登录成功";
                alert(store.userid)
                router.push({
                    name: 'AdminShow',
                    params: {
                        Uid: Uid
                    }
                })
            }
            else if (res.data[0].UR_Quanxian == "管理员") {
                store.userid = "管理员登录成功";
                alert(store.userid)
                router.push({
                    name: 'RegistAdmin',
                    params: {
                        Uid: Uid
                    }
                })
            }
        })
}
</script>